<template>

</template>
<script setup>
	import { doLogin } from '@/utils/common.js'
	import { useglobal } from '@/stores'
	import { reactive } from 'vue';
	
	const launchBtn = ref(null);
	const globalData = useglobal();
	var TIMESTAMP = Math.floor(Date.now() / 1000); // 生成时间戳
	var NONCE_STR = Math.random().toString(36).substr(2, 15); // 生成随机字符串(nonceStr)
	var videoKey = null;
	onLoad(() => {
		let url = window.location.search;
		const code = new URLSearchParams(url).get('code');
		console.log(1111,code)
		doLogin(code).then((res) => {
			console.log('所有的数据', res);
			videoKey = uni.getStorageSync('videoKey')
			console.log('key',videoKey);
			uni.navigateTo({
				url: '/pages/video/video?key=' + videoKey
			})
			// if (!res.phone) { //暂时取返
			// 	uni.navigateTo({
			// 		url: '/pages/video/video?key=' + videoKey
			// 	})
			// } else { //没有手机号获取手机号
			// 	// 对 query 进行编码
			// 	//const token = uni.getStorageSync('third_session');
			// 	const tenantId = res.tenantId;
			// 	const userId = res.id;
			// 	const codedQuery = `appid=wx2305b364aa5ca2cf&tenantId=${tenantId}&userId=${userId}`;
			// 	//const encodedQuery = encodeURIComponent(query);
			// 	// 尝试打开小程序
			// 	console.log('尝试打开小程序携带的参数',codedQuery);
			// 	const miniProgramUrl =
			// 		`weixin://dl/business/?appid=wx2305b364aa5ca2cf&path=pages/login/getPhone&query=${codedQuery}&env_version=trial`
			// 	console.log('尝试打开小程序',miniProgramUrl);
			// 	window.location.href = miniProgramUrl;
			// }

		}).catch(err => {
			console.error('流程出错:', err);
		});
	})


	const configWxJsSdk = () => {
		// console.log(globalData.appid, TIMESTAMP, NONCE_STR, globalData.signature);
		jWeixin.config({
			debug: false,
			appId: globalData.appid,
			timestamp: TIMESTAMP,
			nonceStr: NONCE_STR,
			signature: globalData.signature,
			openTagList: ['wx-open-launch-weapp'],
			success: () => {
				console.log('5微信 JS-SDK 配置成功');
			},
			fail: (err) => {
				console.error('4微信 JS-SDK 配置失败:', err);
			}
		})

		jWeixin.ready(() => {
			console.log('2微信JSSDK配置完成')
		})

		jWeixin.error((err) => {
			console.error('1微信JSSDK配置失败:', err)
			uni.showToast({
				title: '3微信配置失败',
				icon: 'none'
			})
		})

		jWeixin.checkJsApi({
			jsApiList: ['chooseImage'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
			success: function(res) {
				console.log('正常使用', res);
			}
		});
	}
	onShow(() => {
		startCheckingStorage();
	})
	onHide(() => {
		stopCheckingStorage();
	})
	const checkInterval = ref(null)
	const startCheckingStorage = () => {
		stopCheckingStorage();
		// 开始新的检查，每500ms检查一次
		checkInterval.value = setInterval(() => {
			const result = uni.getStorageSync('miniProgramResult');
			if (result) {
				// 清除缓存
				uni.removeStorageSync('miniProgramResult');
				// 停止检查
				this.stopCheckingStorage();
				//获取手机号成功进行页面跳转
				uni.navigateTo({
					url: '/pages/video/video?key=' + videoKey
				})
			}
		}, 500);
	}

	const stopCheckingStorage = () => {
		if (checkInterval.value) {
			clearInterval(checkInterval.value);
			checkInterval.value = null;
		}
	}
</script>

<style lang="scss" scoped>

</style>